import { propType } from "../../levelBundles/level_goldMiner/const/goldMinerConst";
import O1HD from "../gameEngine";
import { CsvFileKind } from "../libs/CsvFileKind";
import CsvDataManage from "./CsvDataManage";
import SuperManager from "./superManager";

export interface goldMinerData{
    hookLevel:number,
}

export default class goldMinerManager extends SuperManager{

    protected className: string = "黄金矿工管理";
    
    async loadManager(): Promise<void> {
        this.loadData();
    }

    _data:goldMinerData = {
        hookLevel:0,
    }

    /**缓存的key */
    private save_data_key = "user-data-gpld-miner-manager-save-key";

    /**初始数据 用于还原 */
    private _initData:string = "";

    /**道具 */
    boomNum:number = 0;
    /**宝箱的提升价格 */
    boxPrice:number = 0;
    /**钻石的提升价格*/
    diamondPrice:number = 0;
    /**所有物品的提升价格 */
    allPrice:number = 0;

    /**加载数据 */
    loadData(){
        this._initData = JSON.stringify(this._data);
        let dataStr = O1HD.getServices().localStorageSrv.getStorage(this.save_data_key) || this._initData;
        let data:goldMinerData = JSON.parse(dataStr);
        this.setData(data);
    }

    /**获取数据 */
    getData(){
        return this._data;
    }

    /**
     * 设置数据
     * @param data 
     * @param isPlus 
     */
    setData(data:goldMinerData,isPlus:boolean = true){
        for(let key in data){
            let d = data[key];
            if(isPlus){
                if( d instanceof Array){
                    this._data[key] = this._data[key].concat(d);
                }else if( typeof d == "number") {
                    this._data[key] += d;
                }else{
                    this._data[key] = d;
                }
            }else{
                this._data[key] = d;
            }
        }
        this.writeData();
    }

    /**写入数据 */
    writeData(){
        O1HD.getServices().localStorageSrv.setStorage(this.save_data_key, JSON.stringify(this._data));
    }

    /**重置manager */
    resetManager(){
        this._data = JSON.parse(this._initData);
        this.writeData();
    }

    /**获取加成 */
    getAdditionByLevel(){
        let dataIds = O1HD.getManagers().csvDataManager.getCsvIds(CsvFileKind.CsvFile.goldMinerHookLevel);
        let level:number = this.getData().hookLevel;
        let addition:number = 0;
        for(let i = 0;i<=level;i++){
            a:for(let j = dataIds.length -1;j>=0; j--){
                let data = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.goldMinerHookLevel,dataIds[j]);
                let startLevel = Number(data["start_level"]);
                if(i >= startLevel){
                    addition += Number(data["level_addition"]);
                    break a;
                }
            }
        }
        return addition;
    }

    addOneProp(prop:propType){
        let addition = Number(O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.goldMinerProp,prop)["use_addition"]);
        switch(prop){
            case propType.BOOM:{
                this.boomNum ++;
                O1HD.emit("refrenshBoom");
                break;
            }
            case propType.CLOVER:{
                this.boxPrice += addition;
                break;
            }
            case propType.GLASS:{
                this.diamondPrice += addition;
                break;
            }
            case propType.VIPCARD:{
                this.allPrice += addition;
                break;
            }
            case propType.UPWRENCH:{
                this.setData({hookLevel:1},true);
                break;
            }
        }
    }

    useBoom(){
        this.boomNum --;
    }


    resetProp(){
        this.boomNum = 0;
        this.allPrice = 0;
        this.diamondPrice = 0;
        this.boxPrice = 0;
    }
    
}
